package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.OrderDao;
import cn.ym.mall.entity.Order;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.xpath.operations.Or;

import java.sql.SQLException;
import java.util.List;

/**
 * 订单 Dao 实现类
 */
public class OrderDaoImpl implements OrderDao {
    QueryRunner queryRunner = new QueryRunner();

    /**
     * 新增订单
     *
     * @param order 添加的订单
     * @return 返回成功的条数
     */
    @Override
    public Integer addOrder(Order order) throws SQLException {
        String sql = "insert into ym_order(uid, sid, gid, cinfoid,ostartTime,oendTime,ostate, ocount, oprice, odelstate) values (?,?,?,?,?,?,?,?,?,?);";
        Object[] params = new Object[]{order.getUid(), order.getSid(), order.getGid(), order.getCinfoid(), order.getOstartTime(), order.getOendTime(),order.getOstate(), order.getOcount(), order.getOprice(), order.getOdelstate()};
        return queryRunner.update(DruidUtils.getConnection(), sql, params);
    }

    /**
     * 根据订单id删除订单
     *
     * @param oid 订单
     * @return 返回删除条数
     */
    @Override
    public Integer deleteByOid(Integer oid) throws SQLException {
        int num = 0;
        try {
            String sql = "delete from ym_order where oid = ?;";
            num = queryRunner.update(DruidUtils.getConnection(), sql, oid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 通过订单id查询订单信息
     *
     * @param oid
     * @return
     */
    @Override
    public Order getOrderByOid(Integer oid) throws SQLException {
        String sql = "select * from ym_order where oid = ?;";
        return queryRunner.query(DruidUtils.getConnection(), sql, new BeanHandler<Order>(Order.class), oid);
    }

    /**
     * 查询订单总数
     *
     * @return 订单总数
     */
    @Override
    public long getOrderCount() throws SQLException {
        long count = 0;
        try {
            String sql = "select count(0) as count from ym_order;";
            count = queryRunner.query(DruidUtils.getConnection(), sql, new ScalarHandler<>());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 通过商店编号统计订单总数
     *
     * @param sid 商店编号
     * @return
     */
    @Override
    public long getCountBySid(Integer sid) throws SQLException {
        long count = 0;
        try {
            String sql = "select count(0) as count from ym_order where sid = ?;";
            count = queryRunner.query(DruidUtils.getConnection(), sql, new ScalarHandler<>(), sid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 通过商家id和订单状态查询订单信息
     *
     * @param sid
     * @return
     * @throws SQLException
     */
    @Override
    public List<Order> getSumBySid(Integer sid) throws SQLException {
        List<Order> orderSidList = null;
        String sql = "select * from ym_order where sid = ? and ostate = 3;";
        try {
            orderSidList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Order>(Order.class),sid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orderSidList;
    }

    /**
     * 分页查询订单
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<Order> getPageOrder(Integer pageNo, Integer pageSize) throws SQLException {
        String sql = "select * from ym_order order by ostartTime desc limit ?,?;";
        List<Order> orderList = null;
        try {
            orderList = queryRunner.query(DruidUtils.getConnection(), sql, new BeanListHandler<Order>(Order.class), (pageNo - 1) * pageSize, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orderList;
    }

    /**
     * 通过商店编号查询订单（分页）
     *
     * @param sid
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<Order> getPageOrderBySid(Integer sid, Integer pageNo, Integer pageSize) throws SQLException {
        String sql = "select * from ym_complaints where sid = ? order by ostartTime desc limit ?,?;";
        List<Order> orderSidList = null;
        try {
            orderSidList = queryRunner.query(DruidUtils.getConnection(), sql, new BeanListHandler<Order>(Order.class),sid, (pageNo - 1) * pageSize, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orderSidList;
    }

    /**
     * 通过用户id查询订单（分页）
     *
     * @param uid
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public List<Order> getPageOrderByuid(Integer uid, Integer pageNo, Integer pageSize) throws SQLException {
        String sql = "select * from ym_order where uid = ? order by ostartTime desc limit ?,?;";
        List<Order> orderList = null;
        try {
            orderList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Order>(Order.class),uid,(pageNo -1)*pageSize,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return orderList;
    }
}
